﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseInterface;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace ObligatorioBD.Logic
{
    class FKCreator
    {
        Connection cnx;
        string catalog;
        Database database;
        Server server;
        Table referneceTable = null;
        Table childTable;
        //bool createDatabase = false;
        ForeignKey fk;

        public FKCreator(Connection cnx, string catalog)
        {
            this.cnx = cnx;
            this.catalog = catalog;
            this.server = new Server(new ServerConnection(cnx.getConnection()));
            this.database = server.Databases[catalog];
        }
        public void SelectReferenceTable(string refTable, string childTable)
        {
            if (this.database == null)
            {
                this.database = server.Databases[catalog];
            }
            this.referneceTable = this.database.Tables[refTable];
            this.childTable = this.database.Tables[childTable];
            this.fk = new ForeignKey(this.childTable, "FK_" + refTable + "_" + childTable);
        }
        public void AddColumnToFK(string columnName)
        {
            ForeignKeyColumn fkc = new ForeignKeyColumn(fk, columnName, columnName);
            this.fk.Columns.Add(fkc);
        }
        public void CreateForeignKeys()
        {
            fk.ReferencedTable = this.referneceTable.Name;
            fk.Create();
        }
    }
}
